/*
 * Barometer.h
 *
 *  Created on: Jan 16, 2014
 *      Author: ppks
 */

#ifndef BAROMETER_H_
#define BAROMETER_H_

#include "config.h"

/******************************************************************************/
/* Barometer Configuration                                                    */
/******************************************************************************/
#if (BAROMETER_TYPE == MS561101BA)
/* Define the I2C address if not defined yet in global config.h */
#ifndef BARO_ADDRESS
    #define BARO_ADDRESS            0x77    /* CBR=0 0xEE I2C address when pin CSB is connected to LOW (GND) */
    //#define MS561101BA_ADDRESS      0x76    /* CBR=1 0xEC I2C address when pin CSB is connected to HIGH (VCC) */
#endif /* #ifndef BARO_ADDRESS  */

/* Registers Definition */
#define MS561101BA_RESET_REG                    0x1E
#define MS561101BA_PRESSURE_REG                 0x40
#define MS561101BA_TEMPERATURE_REG              0x50

/* OSR (Over Sampling Ratio) constants */
#define MS561101BA_OSR_256                      0x00
#define MS561101BA_OSR_512                      0x02
#define MS561101BA_OSR_1024                     0x04
#define MS561101BA_OSR_2048                     0x06
#define MS561101BA_OSR_4096                     0x08

#define OSR_VALUE                               MS561101BA_OSR_4096

#endif /* (BAROMETER_TYPE == MS561101BA) */

/********************************************************************************/
/* Constants Definition                                                         */
/********************************************************************************/
#define BARO_STATE_NO_NEW_DATA                  0
#define BARO_STATE_NEW_DATA_AVAILABLE           1
#define BARO_STATE_NO_NEW_DATA_BUT_COMPUTATION  2

#define BARO_TAB_SIZE                           21
#define BARO_UPDATE_INTERVAL                    25000    /* 40hz update rate (20hz LPF on acc) */

/********************************************************************************/
/* Types Definition                                                             */
/********************************************************************************/
typedef struct {
  /* sensor registers from the MS561101BA datasheet */
  uint16_t c[7];
  union { uint32_t val; uint8_t raw[4]; } ut; /* Uncompensated Temperature */
  union { uint32_t val; uint8_t raw[4]; } up; /* Uncompensated Pressure */
  uint8_t  state;
  uint16_t deadline;
} ms561101ba_s;

/********************************************************************************/
/* Exported Variables Declaration                                               */
/********************************************************************************/

/********************************************************************************/
/* Exported Functions Prototypes                                                */
/********************************************************************************/
extern void Barometer_Init(void);
extern uint8_t Barometer_UpdateData(void);
extern int32_t Barometer_GetPressure(void);
extern int16_t Barometer_GetTemperature(void);
extern uint8_t Barometer_GetEstimatedAltitude(void);

#endif /* BAROMETER_H_ */
